package com.maserb.monitor.service.system_manage.impl;

import com.maserb.monitor.entity.EigenValueDictionaryEntity;
import com.maserb.monitor.repository.EigenValueDictionaryRepository;
import com.maserb.monitor.service.base.impl.JpaSpecificationServiceImpl;
import com.maserb.monitor.service.system_manage.EigenValueDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;

@Service
public class EigenValueDictionaryServiceImpl extends JpaSpecificationServiceImpl<EigenValueDictionaryRepository,
        EigenValueDictionaryRepository, EigenValueDictionaryEntity, String>
        implements EigenValueDictionaryService {

    @Override
    public Page<EigenValueDictionaryEntity> findAll(Pageable pageable, String name) {
        return this.findAll(new Specification<EigenValueDictionaryEntity>() {
            @Override
            public Predicate toPredicate(Root<EigenValueDictionaryEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Path<String> pathName = root.get("name");

                ArrayList<Predicate> list = new ArrayList<Predicate>();
                if (!StringUtils.isEmptyOrWhitespace(name)) {
                    list.add(criteriaBuilder.and(criteriaBuilder.like(pathName, "%" + name + "%")));
                }
                return criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
            }
        }, pageable);
    }
}